Skip to content

refactor: extract ChatCompletionRequest mapping out of handleChatCompletion - #94

Merged
solderzzc merged 1 commit into
mainfrom
refactor/chat-request-parsing
Aug 8, 2026
Merged

refactor: extract ChatCompletionRequest mapping out of handleChatCompletion#94
solderzzc merged 1 commit into
mainfrom
refactor/chat-request-parsing

Conversation

@solderzzc

Copy link
Copy Markdown
Member

Following the test lockdown in PR #93, this refactors the monolithic handleChatCompletion loop.

Changes

  1. Extracts the ChatCompletionRequest.MessageChat.Message mapping loop into a dedicated internal toChatMessage() helper method.
  2. Simplifies the HTTP handler loop to just accumulate systemPromptText.
  3. Updates ChatRequestParsingTests to use the actual production toChatMessage() instead of a mock local copy, perfectly addressing Copilot's previous review suggestion.

Copilot AI review requested due to automatic review settings April 27, 2026 19:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the chat-completions request parsing in Server.swift by extracting the OpenAI message → internal Chat.Message mapping into a dedicated helper, simplifying handleChatCompletion’s message loop.

Changes:

  • Extracted ChatCompletionRequest.MessageChat.Message conversion into toChatMessage().
  • Simplified handleChatCompletion to call toChatMessage() and only accumulate systemPromptText for .system messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2686 to +2690
/// Convert OpenAI JSON spec message to internal Chat.Message
func toChatMessage() -> Chat.Message {
let text = textContent
let imgs = extractImages()
let aud = extractAudio()

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says ChatRequestParsingTests now use the production toChatMessage() helper instead of a local copy, but the test suite in tests/SwiftLMTests/ChatRequestParsingTests.swift still contains its own mapAssistantToolCalls mapping logic and does not call toChatMessage(). This leaves duplicated logic that can drift and means the new helper isn’t actually being locked down by tests. Either update the tests to assert against msg.toChatMessage() (and its toolCalls) or adjust the PR description to match what’s included.

Copilot uses AI. Check for mistakes.
@solderzzc
solderzzc merged commit 38796b8 into main Aug 8, 2026
15 checks passed
@solderzzc
solderzzc deleted the refactor/chat-request-parsing branch August 8, 2026 06:07
@solderzzc

Copy link
Copy Markdown
Member Author

Merged. Reviewed against current main: clean auto-merge, and the code this moves is still verbatim in Server.swift despite heavy churn in that file this week — the churn is all in the streaming path well below the message-mapping loop.

One thing the PR body promised that isn't in the diff: pointing ChatRequestParsingTests at the new toChatMessage() and deleting its private mapAssistantToolCalls duplicate. The refactor landed; the de-duplication that motivated it didn't. Worth a small follow-up so the test exercises the production helper rather than a copy of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants